import java.util.concurrent.atomic.AtomicInteger;

public class ThraedDemo {
    //基于CAS
    public static void main(String[] args) throws InterruptedException {
        AtomicInteger sum=new AtomicInteger(0);
        Thread t1=new Thread( ()-> {
            for (int i = 0; i < 10000; i++) {
                //相当于sum++
                sum.getAndIncrement();
            }
        });
        Thread t2=new Thread( ()-> {
            for (int i = 0; i < 10000; i++) {
                //相当于sum++
                sum.getAndIncrement();
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(sum.get());
    }
}
